package fr.sonictools.xml;

import static org.junit.Assert.assertEquals;

import java.math.BigInteger;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;

import fr.sonictools.test.business.auto_generated.Album;
import fr.sonictools.test.business.auto_generated.Author;
import fr.sonictools.test.business.auto_generated.Discotheque;
import fr.sonictools.test.business.auto_generated.Track;

public class CommonTestClass {
	public static final String PACKAGE_CONTAINING_BUSINESS_CLASSES="fr.sonictools.test.business.auto_generated";
	public static final String RESOURCES_PATH="./resources";
	public static final String INPUT_XML="discotheque.xml";
	public static final String OUTPUT_XML="discotheque_out.xml";
	
	public static Discotheque createExpectedDiscotheque(){
		Discotheque discoTest = new Discotheque();
		
        Author author1 = new Author();
        author1.setFirstname("firstname1");
        author1.setName("name1");
        
        Author author2 = new Author();
        author2.setFirstname("firstname2");
        author2.setName("name2");

        Track track1_album1 = new Track();
        track1_album1.setValue("track1_album1");
        track1_album1.setNumber(BigInteger.valueOf(1));
        track1_album1.setLength("3:01");

        Track track2_album1 = new Track();
        track2_album1.setValue("track2_album1");
        track2_album1.setNumber(BigInteger.valueOf(2));
        track2_album1.setLength("2:09");

        Track track3_album1 = new Track();
        track3_album1.setValue("track3_album1");
        track3_album1.setNumber(BigInteger.valueOf(3));
        track3_album1.setLength("4:22");

        Track track4_album1 = new Track();
        track4_album1.setValue("track4_album1");
        track4_album1.setNumber(BigInteger.valueOf(4));
        track4_album1.setLength("3:28");

        Track track1_album2 = new Track();
        track1_album2.setValue("track1_album2");
        track1_album2.setNumber(BigInteger.valueOf(1));
        track1_album2.setLength("3:01");

        Track track2_album2 = new Track();
        track2_album2.setValue("track2_album2");
        track2_album2.setNumber(BigInteger.valueOf(2));
        track2_album2.setLength("2:09");

        Track track3_album2 = new Track();
        track3_album2.setValue("track3_album2");
        track3_album2.setNumber(BigInteger.valueOf(3));
        track3_album2.setLength("4:22");

        Track track4_album2 = new Track();
        track4_album2.setValue("track4_album2");
        track4_album2.setNumber(BigInteger.valueOf(4));
        track4_album2.setLength("3:28");

        Album album1 = new Album();
        try {
			album1.setDate(DatatypeFactory.newInstance().newXMLGregorianCalendarDate(2001, 1, 1, +1));
		} catch (DatatypeConfigurationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		album1.setTitle("album1");
		album1.setAuthor(author1);
        album1.getTrack().add(0,track1_album1);
        album1.getTrack().add(1,track2_album1);
        album1.getTrack().add(2,track3_album1);
        album1.getTrack().add(3,track4_album1);

        Album album2 = new Album();
        try {
			album2.setDate(DatatypeFactory.newInstance().newXMLGregorianCalendarDate(2002, 2, 2, +1));
		} catch (DatatypeConfigurationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		album2.setTitle("album2");
		album2.setAuthor(author2);
        album2.getTrack().add(0,track1_album2);
        album2.getTrack().add(1,track2_album2);
        album2.getTrack().add(2,track3_album2);
        album2.getTrack().add(3,track4_album2);



        discoTest.getAlbum().add(0,album1);
        discoTest.getAlbum().add(1,album2);
        
        return discoTest;
    }
	
	public static void compareDiscoObjects(Discotheque expDisco, Discotheque resultDisco){
        assertEquals(expDisco.getAlbum().size(), resultDisco.getAlbum().size());
        for(int albIdx=0; albIdx<expDisco.getAlbum().size(); albIdx++){
        	assertEquals(expDisco.getAlbum().get(albIdx).getTitle(), resultDisco.getAlbum().get(albIdx).getTitle());            
        	assertEquals(expDisco.getAlbum().get(albIdx).getDate(), resultDisco.getAlbum().get(albIdx).getDate());            
        	assertEquals(expDisco.getAlbum().get(albIdx).getAuthor().getFirstname(), resultDisco.getAlbum().get(albIdx).getAuthor().getFirstname());
            assertEquals(expDisco.getAlbum().get(albIdx).getAuthor().getName(), resultDisco.getAlbum().get(albIdx).getAuthor().getName());
            
        	assertEquals(expDisco.getAlbum().get(albIdx).getTrack().size(), resultDisco.getAlbum().get(albIdx).getTrack().size());
            for(int trkIdx=0; trkIdx<expDisco.getAlbum().get(0).getTrack().size(); trkIdx++){
                assertEquals(expDisco.getAlbum().get(albIdx).getTrack().get(trkIdx).getValue(), resultDisco.getAlbum().get(albIdx).getTrack().get(trkIdx).getValue());
                assertEquals(expDisco.getAlbum().get(albIdx).getTrack().get(trkIdx).getLength(), resultDisco.getAlbum().get(albIdx).getTrack().get(trkIdx).getLength());
                assertEquals(expDisco.getAlbum().get(albIdx).getTrack().get(trkIdx).getNumber(), resultDisco.getAlbum().get(albIdx).getTrack().get(trkIdx).getNumber());
            }
        }
    }
}
